<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Buy extends CI_Controller {
    function Buy() 
    {
        parent::__construct();
        //$this->load->scaffolding('entries');
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('file');
        $this->load->helper('captcha');
        $this->load->helper(array('form', 'url'));
    }
    
    public function index($msg = NULL)
    {
        $data['message'] = "";

        //Database
        //$data['category']=$this->db->get('psacar_category');

        $this->db->where('type_id', 3);
        $this->db->order_by('company', 'asc');
        $data['dealer']= $this->db->get('psacar_member');
        
        $data['type']= $this->db->get('psacar_car_type');        
        $data['make']= $this->db->get('psacar_make');
        $data['model']= $this->db->get('psacar_model');
        

        $this->db->order_by("order", "asc"); 
        $this->db->order_by("province", "asc"); 
        $data['location'] = $this->db->get('psacar_location');
        
        //Query
        $this->db->group_by('year'); // Order by
        $this->db->group_by('price'); // Order by
        
        $this->load->view('header', $data);
        $this->load->view('car', $data);
        $this->load->view('footer', $data);
    }
    
    public function detail()
    {
        $data['message'] = "";

        //Variable
        $car_id = $this->uri->segment(3);        
        
        //Query: Car list
        $this->load->model('Car_model');
        $data['car_list'] = $this->Car_model->get_car_by_id($car_id);

        //Query: Comment
        $this->load->model('Comment_model');
        $data['comment_list'] = $this->Comment_model->get_comments_by_car($car_id);	

        $this->load->view('header', $data);
        $this->load->view('templates/car/car_detail', $data);
        
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer', $data);
    }    
    
    public function process()
    {		
        $email = $this->security->xss_clean($this->input->post('email'));
        $password = $this->security->xss_clean($this->input->post('password'));
         
        $this->load->model('Member_model');
        $this->Member_model->login($email, $password);
        if($this->session->userdata('validated'))
            $data['message'] = "";
        else
            $data['message'] = "<font color=red>Invalid username and/or password.</font>";
                                    
        $car_id = $this->uri->segment(3);
        if($car_id)
        {
            $this->load->model('Car_model');
            $data['car_list'] = $this->Car_model->get_car_by_id($car_id);

            $this->load->model('Comment_model');
            $data['comment_list'] = $this->Comment_model->get_comments_by_car($car_id);

            $this->load->view('header', $data);
            $this->load->view('templates/car/car_detail', $data);
            $data['make']= $this->db->get('psacar_make');
            $this->load->view('footer',$data);        
        }
        else 
            redirect(base_url().'index.php/car');
    } 
    
    public function result()
    {
        $data['message'] = "";
        
        $offset = $this->uri->segment(3);
        if(strlen($offset) == 0)
            $offset = 0;
        
        $limit = $this->input->post('limit');
        if(strlen($limit) == 0)
            $limit = 10;        

        $cartype = '%';
        if($this->input->post('type'))
            $cartype = $this->input->post('type');
        $make = $this->input->post('make');
        $model = $this->input->post('model');
        $maxprice = intval($this->input->post('maxprice'));
        $location = $this->input->post('location');
        $dealer = $this->input->post('dealer');
        $reference_number = $this->input->post('reference_number');
        
        $data['criteria_list'] = 
            $this->register_search_criteria(                    
                $cartype, 
                $make, 
                $model, 
                $maxprice, 
                $location, 
                $dealer, 
                '', 
                '', 
                '', 
                '', 
                '', 
                '', 
                '',
                '',
                $reference_number);
        
        $this->load->model('Car_model');
        $car_list = $this->Car_model->get_car_list(
            $make, 
            $model, 
            $maxprice, 
            $dealer,
            '',
            $location,
            $cartype,
            '',
            '',
            '',
            '',
            $reference_number);
        
        //Pagination
        $data['pagination'] = $this->generate_pagination(
            'index.php/buy/result',
            $car_list->num_rows,
            $limit);
        
        $car_list = array_slice($car_list->result(), $offset, $limit);
        $data['car_list'] = $car_list;
        
        $this->load->view('header', $data);
        $this->load->view('templates/car/car_search_result', $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer',$data);        
    }
    
    public function register_search_criteria(
        $cartype = '%',
        $make = '',
        $model = '',
        $maxprice = '',
        $location = '',
        $dealer = '',
        $price_range = '',
        $mileage_range = '',
        $year_range = '',
        $external_color = '',
        $drivetrain = '',
        $transmission = '',
        $fuel = '',
        $category = '',
        $reference_number) {
        
        $search_critieria = array(
            array(
                'field' => 'cartype',
                'value' => $cartype),
            array(
                'field' => 'make',
                'value' => $make),
            array(
                'field' => 'model',
                'value' => $model),
            array(
                'field' => 'maxprice',
                'value' => intval($maxprice)),
            array(
                'field' => 'location',
                'value' => $location),
            array(
                'field' => 'dealer',
                'value' => $dealer),
            array(
                'field' => 'price_range',
                'value' => $price_range),
            array(
                'field' => 'mileage_range',
                'value' => $mileage_range),
            array(
                'field' => 'year_range',
                'value' => $year_range),
            array(
                'field' => 'transmission',
                'value' => $transmission),
            array(
                'field' => 'fuel',
                'value' => $fuel),            
            array(
                'field' => 'category',
                'value' => $category),            
            array(
                'field' => 'reference_number',
                'value' => $reference_number),            
        );

        return $search_critieria;
    }
    
    public function generate_pagination($base_url = '', $total_row = 0, $per_page = 0) {       
        $this->load->library('pagination');
        $config['base_url'] = base_url() . $base_url;
        $config['total_rows'] = $total_row;
        $config['per_page'] = $per_page;

        $limiter = '<select name="limit">';
        for($counter = 10; $counter <= 100; ) {
            $limiter .= 
                '<option value="' . $counter . '" ' . 
                ($per_page == $counter ? 'selected' : '') . 
                '>' . $counter . ' per page</option>';
            $counter += 10;
        }
        $limiter .= "</select> | ";
                
        $config['full_tag_open'] = '<div class="pagination">' . $limiter;
        $config['full_tag_close'] = '</div>';
        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }    
}